<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">
	</head>
	<body>

		<div class="layui-fluid">
			<div class="layui-card">
				<div class="layui-card-header table-operate-btn">
					<!-- <span class="marign_right10">全部代理商</span> -->
					<button class="layui-btn layui-btn-normal layui-btn-sm" data-type='alladd'>添加</button>
					<button class="layui-btn layui-btn-danger layui-btn-sm" data-type='alldel'>删除</button>
				</div>
				<div class="layui-card-body">
					<!-- 搜索 -->
					<form class="layui-form search" id="form" onsubmit="return false" action="##">
						<div class="layui-inline search_inline">
							<label class="layui-form-label">消息类型：</label>
							<div class="layui-input-inline">
								<select name="type" class="messages_type"></select>
							</div>
						</div>
						<div class="layui-inline search_inline">
							<label class="layui-form-label">消息状态：</label>
							<div class="layui-input-inline">
								<select name="status" class="messages_status"></select>
							</div>
						</div>
						<div class="layui-inline search_inline">
							<label class="layui-form-label">会员ID：</label>
							<div class="layui-input-inline">
								<input class="layui-input" placeholder="会员ID" name="customerid" autocomplete="off">
							</div>
						</div>
						<div class="layui-inline search_inline">
							<label class="layui-form-label">手机号码：</label>
							<div class="layui-input-inline">
								<input class="layui-input" placeholder="手机号码" name="keyword" autocomplete="off">
							</div>
						</div>
						<!-- <div class="layui-inline search_inline">
							<label class="layui-form-label">归属伙伴：</label>
							<div class="layui-input-inline">
								<input class="layui-input" placeholder="归属伙伴名称" name="keyword" autocomplete="off">
							</div>
						</div> -->
						<button class="layui-btn search_inline" lay-submit="" lay-filter="search" data-type="reload">搜索</button>
						<button class="layui-btn layui-btn-primary search_inline" type="reset">重置</button>
					</form>
					<!-- 搜索 -->

					<table id="test-table-operate" lay-filter="test-table-operate"></table>
					<script type="text/html" id="test-table-operate-barDemo">
						{{#  if(d.type != 2){ }}
							<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
						{{#  } }}
					   <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> -->
					</script>
				</div>
			</div>
		</div>

		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/common.js" type="text/javascript" charset="utf-8"></script>
		<script>
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'table', 'laytpl'], function() {
				var $ = layui.$,
					form = layui.form,
					laytpl = layui.laytpl,
					table = layui.table;

				initdata()
				var _data = {};

				messages_type_show('option')
				messages_status_show('option')

				//用户管理
				function initdata() {
					table.render({
						elem: '#test-table-operate',
						url: server.querymessages,
						height: 'full-105',
						defaultToolbar: [  'exports', {
							title: '提示' //标题
							, layEvent: 'LAYTABLE_TIPS' //事件名，用于 toolbar 事件中使用
							, icon: 'layui-icon-tips' //图标类名
						}],
						toolbar: true,
						cols: [
							[{
									type: 'checkbox',
									fixed: 'left'
								},
								{
									width: 120,
									align: 'center',
									toolbar: '#test-table-operate-barDemo',
									title: '操作',
									fixed: 'left'
								}, {
									field: 'status',
									width: 150,
									title: '状态',
									templet: function(d) {
										return String(d.status) ? messages_status_show(1, d.status) : ''
									}
								}, {
									field: 'type',
									width: 150,
									title: '类型',
									templet: function(d) {
										return d.type ? messages_type_show(1, d.type) : ''
									}
								}, {
									field: 'title',
									width: 150,
									title: '标题',
									sort: true
								}, {
									field: 'content',
									title: '内容',
									minWidth: 100
								}, {
									field: 'memberid',
									title: '会员ID',
									width: 100,
									templet: '#imgTpl'
								}
							]
						],
						page: true,
						where: _data,
						limit: limit,
						limits: limits,
						request: {
							pageName: 'page',
							limitName: 'size',
						},
						parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
							return {
								"code": res.code, //解析接口状态
								"msg": res.msg, //解析提示文本
								"count": res.messages.pager.total, //解析数据长度
								"data": res.messages.data //解析数据列表
							};
						}
					});
				}

				//监听搜索
				form.on('submit(search)', function(data) {
					_data = data.field;
					initdata();
					return false;
				});

				$("button[type=reset]").on("click", function() {
					_data = {}
					initdata();
					table.reload('test-table-operate');
					document.getElementById("form").reset();
				});

				//监听表格复选框选择
				table.on('checkbox(test-table-operate)', function(obj) {
					console.log(obj)
				});

				//监听工具条
				table.on('tool(test-table-operate)', function(obj) {
					var data = obj.data;
					if (obj.event === 'del') {
						layer.confirm('真的删除行么', function(index) {
							obj.del();
							layer.close(index);
						});
					} else if (obj.event === 'edit') {
						layer.open({
							type: 2,
							title: '编辑',
							shadeClose: false,
							shade: 0.5,
							maxmin: true, //开启最大化最小化按钮
							area: ['50%', '70%'],
							content: 'model/edit_add.html?id=' + data.id,
							end: function() {}
						});
					}
				});

				//事件
				var active = {
					alldel: function() {
						var checkStatus = table.checkStatus('test-table-operate'),
							data = JSON.stringify(checkStatus.data);

						
						var id = ''
						for (var i = 0; i < checkStatus.data.length; i++) {
							id += checkStatus.data[i].id
							if(i != checkStatus.data.length - 1){
								id += ','
							}
						}
						
						if (checkStatus.data.length <= 0) {
							layer.msg('请选择操作行')
						} else {
							layer.confirm('真的删除所选的'+ checkStatus.data.length +'条数据么?', function(index) {
								$.post(server.delmessages, { id: id }, function(res, status) {
									if (status == 'success') {
										if (res.code == 0) {
											layer.alert('操作成功', function() {
												layer.closeAll();
												layui.table.reload('test-table-operate');
											});
										} else {
											layer.msg(res.msg);
											layer.closeAll('loading');
										}
									} else {
										layer.msg('服务器连接错误');
									}
								});
							});
						}
					},
					alladd: function() {
						layer.open({
							type: 2,
							title: '添加',
							content: 'model/edit_add.html',
							area: ['60%', '60%'],
							yes: function(index, layero) {
								var iframeWindow = window['layui-layer-iframe' + index],
									submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

								//监听提交
								iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data) {
									var field = data.field; //获取提交的字段

									//提交 Ajax 成功后，静态更新表格中的数据
									//$.ajax({});              
									table.reload('LAY-user-back-role');
									layer.close(index); //关闭弹层
								});

								submit.trigger('click');
							}
						});
					}
				}
				$('.table-operate-btn .layui-btn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
			});
		</script>
	</body>
</html>
